import { MiddlewareConsumer, Module, NestModule, RequestMethod } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PostsModule } from './posts/posts.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import config from 'config/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { LoggerMiddleware } from './middeware/logger.middleware';
import { PostsController } from './posts/posts.controller';


@Module({
  imports: [ConfigModule.forRoot({
      /** 全局使用 将ConfigModule加载到根模块（例如AppModule）后，您无需在其他模块中导入它 */
    isGlobal:true,
    load:[config]
  }),
  TypeOrmModule.forRootAsync({
    imports: [ConfigModule], // 记得导入 ConfigModule
    /** 配置的使用 */
    useFactory: async (configService: ConfigService) => ({
      type: 'mysql',
      host: configService.get<string>('database.host'),
      port: configService.get<number>('database.port'),
      username: configService.get<string>('database.username'),
      password: configService.get<string>('database.password'),
      database: configService.get<string>('database.database'),
      synchronize: true,
      autoLoadEntities: true,
      timezone: '+08:00', // 东八时区
    }),
    inject: [ConfigService],
  }),
  PostsModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule  implements NestModule{
  configure(consumer: MiddlewareConsumer) {
    // consumer.apply(LoggerMiddleware)
    // .forRoutes({path:"posts",method:RequestMethod.GET})
    // .forRoutes(PostsController) // 也可以使用控制器
  }
  
}
